﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace XTYAPI.PLC
{
    /// <summary>
    /// 用于波动频率较高的场合
    /// </summary>
    public class 一阶滤波
    {
        /// <summary>
        /// 默认权重
        /// </summary>
        private double scale=0.5;
        /// <summary>
        /// 一阶滤波结果
        /// </summary>
        public double Result { get;private set; }
        /// <summary>
        /// 计算次数
        /// </summary>
        public double CalCount { get;private set; }
        /// <summary>
        /// 设置滤波权重，靠近0滞后，靠近1灵敏
        /// </summary>
        public double Scale
        {
            get { return scale; }
            set
            {
                if (value>1||value<=0)
                {
                    throw new Exception("一阶滤波算法的权重值在0到1之间，包含0和1");
                }
                else
                {
                    scale = value;
                }
            } 
        }
        /// <summary>
        /// 计算滤波结果
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public double CalValue(double value)
        {
            Result = Scale * value + (1 - Scale) * Result;
            CalCount++;
            return Result;
        }
    }
}
